package com.whm.hrmanagement.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.whm.common.core.domain.entity.SysDept;
import com.whm.common.core.domain.entity.SysUser;
import com.whm.common.core.domain.model.LoginUser;
import com.whm.common.utils.DateUtils;
import com.whm.system.mapper.SysDeptMapper;
import com.whm.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.whm.hrmanagement.mapper.OaTargetTrackingMapper;
import com.whm.hrmanagement.domain.OaTargetTracking;
import com.whm.hrmanagement.service.IOaTargetTrackingService;

import static com.whm.common.utils.PageUtils.startPage;

/**
 * 个人计划表Service业务层处理
 * 
 * @author whm
 * @date 2024-02-13
 */
@Service
public class OaTargetTrackingServiceImpl implements IOaTargetTrackingService 
{
    @Autowired
    private OaTargetTrackingMapper oaTargetTrackingMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    /**
     * 查询个人计划表
     * 
     * @param id 个人计划表主键
     * @return 个人计划表
     */
    @Override
    public OaTargetTracking selectOaTargetTrackingById(Long id)
    {
        return oaTargetTrackingMapper.selectOaTargetTrackingById(id);
    }

    /**
     * 查询个人计划表列表
     * 
     * @param oaTargetTracking 个人计划表
     * @return 个人计划表
     */
    @Override
    public List<OaTargetTracking> selectOaTargetTrackingList(OaTargetTracking oaTargetTracking)
    {
        LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user.getDeptId() != 204 && user.getDeptId() != 200 &&  user.getUserId() != 109 && user.getUserId() != 1) {
            List<OaTargetTracking> oaTargetTrackings = oaTargetTrackingMapper.selectOaTargetTrackingList(oaTargetTracking);

            return oaTargetTrackings.stream().filter(e -> e.getName() == user.getUserId()).collect(Collectors.toList());
        }
        startPage();
        List<OaTargetTracking> oaTargetTrackings = oaTargetTrackingMapper.selectOaTargetTrackingList(oaTargetTracking);
        return oaTargetTrackingMapper.selectOaTargetTrackingList(oaTargetTracking);
    }

    /**
     * 新增个人计划表
     * 
     * @param oaTargetTracking 个人计划表
     * @return 结果
     */
    @Override
    public int insertOaTargetTracking(OaTargetTracking oaTargetTracking)
    {
        oaTargetTracking.setCreateTime(DateUtils.getNowDate());
        SysUser sysUser = sysUserMapper.selectUserById(oaTargetTracking.getName());
        oaTargetTracking.setNameTxt(sysUser.getNickName());
        SysDept sysDept = sysDeptMapper.selectDeptById(oaTargetTracking.getDepartmentName());
        oaTargetTracking.setDepartmentNameTxt(sysDept.getDeptName());
        return oaTargetTrackingMapper.insertOaTargetTracking(oaTargetTracking);
    }

    /**
     * 修改个人计划表
     * 
     * @param oaTargetTracking 个人计划表
     * @return 结果
     */
    @Override
    public int updateOaTargetTracking(OaTargetTracking oaTargetTracking)
    {
        oaTargetTracking.setUpdateTime(DateUtils.getNowDate());
        SysUser sysUser = sysUserMapper.selectUserById(oaTargetTracking.getName());
        oaTargetTracking.setNameTxt(sysUser.getNickName());
        SysDept sysDept = sysDeptMapper.selectDeptById(oaTargetTracking.getDepartmentName());
        oaTargetTracking.setDepartmentNameTxt(sysDept.getDeptName());
        return oaTargetTrackingMapper.updateOaTargetTracking(oaTargetTracking);
    }

    /**
     * 批量删除个人计划表
     * 
     * @param ids 需要删除的个人计划表主键
     * @return 结果
     */
    @Override
    public int deleteOaTargetTrackingByIds(Long[] ids)
    {
        return oaTargetTrackingMapper.deleteOaTargetTrackingByIds(ids);
    }

    /**
     * 删除个人计划表信息
     * 
     * @param id 个人计划表主键
     * @return 结果
     */
    @Override
    public int deleteOaTargetTrackingById(Long id)
    {
        return oaTargetTrackingMapper.deleteOaTargetTrackingById(id);
    }
}
